<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/12/5
 * Time: 14:31
 */
namespace app\index\controller;

use app\common\model\MsgModel;
use app\admin\model\OpinionModel;
use app\common\model\IntegralModel;
use app\common\model\ProjectModel;
use app\common\model\ReplyModel;
use think\Db;

class Message extends Common
{

    //消息列表
    public function index()
    {
		
		
		$user = Db::table('dx_user')->where('openid',$_SESSION['openid'])->find();
	
        //获取未查看的项目
        $on_look_project = ProjectModel::where('is_look', '0')->where('user_id', $user['user_id'])->order('sysdate desc')->select();
        //获取积分未查看
        $no_look_integral = IntegralModel::where('is_look', '0')->where('user_id', $user['user_id'])->order('sysdate desc')->select();
        //获取未查看回复
        $opinion = OpinionModel::where('user_id', $user['user_id'])->select();
		//查询msg
		$msg = MsgModel::where('user_id',$user['user_id'])->order('created_at desc')->select();
        $no_look_message = [];
        //已查看的回复
        $look_message = [];
        foreach ($opinion as $k => $v) {
            $re = ReplyModel::where('opinion_id', $v->id)->where('is_look', '0')->find();
            if ($re) {
                if ($v->type == '1') {
                    //项目反馈
                    $types = feedback_type_1();
                }
                if ($v->type == '2') {
                    //普通反馈
                    $types = feedback_type_2();
                }
                $re['title'] = $types[$v->title];
                $re['address'] = $v->project_id;
                $no_look_message[] = $re;
            }
            //获取已查看的回复
            $look = ReplyModel::where('opinion_id', $v->id)->where('is_look', '1')->find();
            if ($look) {
                if ($v->type == '1') {
                    //项目反馈
                    $types = feedback_type_1();
                }
                if ($v->type == '2') {
                    //普通反馈
                    $types = feedback_type_2();
                }
                $look['title'] = $types[$v->title];
                $look['address'] = $v->project_id;
                $look_message[] = $look;
            }
        }
  
        $this->assign('no_look_message', $no_look_message);
        $this->assign('no_look_integral', $no_look_integral);
        $this->assign('look_message',$look_message);
        $this->assign('on_look_project', $on_look_project);
		$this->assign('msg', $msg);

        return view('msg');
    }

    //查看详细的具体内容
    public function project_details($id, $type)
    {
        if (!$id) {
            $this->error('没有id值');
        }

        if ($type == 'project') {
            //项目
            $re = ProjectModel::get($id);
            $re->title = '项目状态变化';
            $re->content = $re->address . '<br/>项目状态:' . $re->prostatus . '<br/>积分状态:' . $re->linkstatus;
            $up = ProjectModel::get($id);
            $up->is_look = '1';
            $up->updated_at = time();
            $up->save();

        } elseif ($type == 'integral') {
            //积分
            $re = IntegralModel::get($id);
            $re->title = '项目积分变化';
            $re->content = $re->address . '<br/>积分:' . $re->point;
            $up = IntegralModel::get($id);
            $up->is_look = '1';
            $up->updated_at = time();
            $up->save();

        } elseif ($type == "message") {
            //查询回复
            $re = ReplyModel::get($id);
            //查询反馈信息
            $opinion = OpinionModel::get($re->opinion_id);
            if ($opinion) {
                if ($opinion->type == '1') {
                    //项目反馈
                    $types = feedback_type_1();
                }
                if ($opinion->type == '2') {
                    //普通反馈
                    $types = feedback_type_2();
                }
                $re->address = $opinion->project_id;
                $re->title = '关于' . $opinion->project_id . $types[$opinion->title] . '的意见回复';
                //更新查看状态;
                $up = ReplyModel::get($id);
                $up->is_look = '1';
                $up->look_time = time();
                $up->save();

            } else {
                $this->error('没有查询到反馈');
            }
        }elseif ($type == "tuisong"){
			
				$re = MsgModel::get($id);
				$re->title = '消息推送';
				
		}else {
                $this->error('没有类型值');
            }

        $this->assign('re', $re);
        return view('msgdetails');
    }





}